package com.hp.spring3apptest.spring.ui.controllers;

import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;
import org.hibernate.validator.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.hp.spring3apptest.spring.ui.components.StudentLogin;
import com.hp.spring3apptest.spring.ui.validators.StudentLoginValidator;










/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping(value = "/Student")
public class StudentController {

    /**
     * Simply selects the home view to render by returning its name.
     */

    StudentLoginValidator validator = null;

    public StudentLoginValidator getValidator() {
        return validator;
    }

    @Autowired
    public void setValidator(StudentLoginValidator validator) {
        this.validator = validator;
    }

    /*
     * private ClassValidator<StudentLogin> studentValidator = new ClassValidator<StudentLogin>(StudentLogin.class); public void
     * setValidator(ClassValidator<StudentLogin> validator) { this.studentValidator = validator; }
     */

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(@ModelAttribute("key_stuLogin") StudentLogin user, BindingResult result) {

        // validateUser(user);

        validator.validate(user, result);

        if (result.hasErrors()) {
            System.out.println("has errors");
            return "home";
        }

        System.out.println(user.getUserName());
        System.out.println(user.getUserPassword());

        return "home";
    }

    /*
     * private void validateUser(StudentLogin user) { InvalidValue[] invalidValues = studentValidator.getInvalidValues(user); for
     * (InvalidValue value : invalidValues) { System.out.println("========"); System.out.println(value); System.out.println("message=" +
     * value.getMessage()); System.out.println("propertyName=" + value.getPropertyName()); System.out.println("propertyPath=" +
     * value.getPropertyPath()); System.out.println("value=" + value.getValue()); } }
     */

}
